﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace HelloXNA2.Component._3D._3DModel1
{
    /// <summary>
    /// 自定义 3D 模型类
    /// </summary>
    public class MyModel
    {
        private Model _model;

        private Matrix _rotation = Matrix.Identity;

        private Matrix _world = Matrix.Identity;

        public MyModel(Model model, Matrix world)
        {
            _model = model;
            _world = world;
        }


        public void Draw(MyCamera camera)
        {
            foreach (ModelMesh mesh in _model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();

                    effect.Projection = camera.Projection;
                    effect.View = camera.View;
                    effect.World = GetWorld();
                }

                // 绘制 ModelMesh
                mesh.Draw();
            }
        }

        public void Rotate(RotationDirection direction)
        {
            switch (direction)
            {
                case RotationDirection.Up:
                    _rotation *= Matrix.CreateRotationX(-MathHelper.Pi / 180); // 绕 X 轴旋转的弧度 
                    break;
                case RotationDirection.Down:
                    _rotation *= Matrix.CreateRotationX(MathHelper.Pi / 180);
                    break;
                case RotationDirection.Left:
                    _rotation *= Matrix.CreateRotationY(-MathHelper.Pi / 180); // 绕 Y 轴旋转的弧度 
                    break;
                case RotationDirection.Right:
                    _rotation *= Matrix.CreateRotationY(MathHelper.Pi / 180);
                    break; 

            }
        }

        private Matrix GetWorld()
        {
            return _world * _rotation;
        }
    }
}
